//
//  HMComplicationTableViewController.m
//  仿真开发
//
//  Created by jieChen on 17/3/24.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMComplicationTableViewController.h"
#import "HMTool.h"
#import "AppointModel.h"
#import <YYModel.h>
#import "MJRefresh.h"

@interface HMComplicationTableViewController ()

@property (nonatomic,strong) NSMutableArray *dataArr;

@property (strong,nonatomic) NSMutableDictionary *conDict;

@property (nonatomic,strong) NSMutableArray *indexPath;

@end

static NSString *complicationID = @"cell";
@implementation HMComplicationTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:1.3];
        [self.tableView.mj_header endRefreshing];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:1.3];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
    
    _dataArr = [NSMutableArray array];
     _illArr = [NSMutableArray array];
    _indexPath = [NSMutableArray array];
    // 注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:complicationID];
    [self loadData];
}


//- (void)viewWillAppear:(BOOL)animated {
//    
//    [super viewWillAppear:animated];
//    
//    id obj = [[NSUserDefaults standardUserDefaults] valueForKey:@"EZ"];
//    
//    NSLog(@"%@",obj);
//}

- (void)loadData {
    NSString *url = @"http://iosapi.itcast.cn/doctor/complicationList.json.php";
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@1,@"ci2_id":@3};
    [[HMTool sharedNetworkTool] POSTWithURLString:url parameters:parameters success:^(id responseObject) {
        // 解析json
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
        //        NSLog(@"%@",responseObject);
        
        NSArray *data = dic[@"data"];
        //        NSLog(@"%@",data);
        
        NSArray *modelArr = [NSArray yy_modelArrayWithClass:[AppointModel class] json:data];
        //        NSLog(@"%@",modelArr);
        _dataArr = [NSMutableArray arrayWithArray:modelArr];
//        NSLog(@"%@",_dataArr);
        
        // 在主线程刷新ui
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } faile:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:complicationID forIndexPath:indexPath];
    
    AppointModel *model = _dataArr[indexPath.row];
    
    cell.textLabel.text = model.complication_name;
    
    return cell;
}

#pragma mark - table view datagate
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AppointModel *mode = _dataArr[indexPath.row];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *btn;
    
    UIBarButtonItem *navBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(popTolast)];
    self.navigationItem.rightBarButtonItem = navBtn;

    
    // 如果数组包含这个模型
    // 1.让按钮父视图移除
    // 2.让模型从数组移除
    if ([self.illArr containsObject:mode]) {
        
        for (UIView *view in cell.contentView.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)view;
                [btn removeFromSuperview];
                [self.illArr removeObject:mode];
                [self.indexPath removeObject:indexPath];

            }
        }
        
    } else {
        
        btn = [[UIButton alloc]init];
        [btn setImage:[UIImage imageNamed:@"login_protocol"] forState:UIControlStateNormal];
        [cell.contentView addSubview:btn];
        btn.frame = CGRectMake(330, 10, 30, 30);
        [self.illArr addObject:mode];
        [self.indexPath addObject:indexPath];
    }
    

    
    
   

    
}

-(void)popTolast{
    [self.delegate HMComplicationTableViewController:self andillName:_illArr];
    
//    [[NSUserDefaults standardUserDefaults] setValue:self.illArr forKey:@"EZ"];
    
    
    
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
